<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\17 0017
 * Time: 11:29
 */
namespace model;
use model;
class goodsModel extends model{


    public static function search( $params ){


        $sql = "select g.*,cp.* from sh_goods g";

        $where = '';
        $sql .= " left join sh_goods_to_category g2c on g.id=g2c.goods_id  
            left join sh_category_path cp on g2c.category_id = cp.category_id";
        if( !empty($params['category_id'] ) ) {



            $where .= "  cp.path_id=" . $params['category_id'];
        }


        if( !empty( $params['key'] ) ){
            $where .= "  g.`name` like '%" . $params['key'] . "%' ";
        }

        if( !empty( $where ) ){

            $sql.= " where " . $where;
        }


        $result = self::$db->query( $sql );

        return $result->rows;

    }

    public static function detail( $goods_id ){

        $sql = "select * from sh_goods g 
                  left join sh_goods_description gd on g.id=gd.goods_id
                  where g.id=" . $goods_id;
        $result = self::$db->query( $sql );

        return $result->row;

    }

    public static function image_list( $goods_id ){

        $sql = "select * from sh_goods_img 
                  where goods_id=" . $goods_id;

        $result = self::$db->query( $sql );

        return $result->rows;

    }

    public static function getImgUrl( $goods_id ){

        $sql = 'select url from sh_goods where is_default=1 and goods_id= ' . $goods_id . ' limit 0,1';
        $result = self::$db->query( $sql );
        return !empty( $result->row ) ? $result->row['url']:'';
    }

    /**
     * @param $data
     * @return string
     * 测试， 之后删掉
     */
    public static function add( $name, $price ){

        $sql = 'insert into sh_goods set price=' . $price .', name="'.$name . '"';
        $result = self::$db->query( $sql );
        return $result;
    }

    /**
     * @param $data
     * @return string
     * 测试， 之后删掉
     */
    public static function delete( $product_id ){

        $sql = 'delete from sh_goods where id=' . $product_id;
        $result = self::$db->query( $sql );
        return $result;
    }

}